package com.wdz.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

/**
 * 阿里云发送短信
 *
 * @author koosky
 * @create 2019-04-08
 **/
@Slf4j
public class AliyunSmsUtils {

    private static final String  accessKeyId = "LTAIewWWFgRViLCE";
    private static final String  accessKeySecret = "2rG8xMnTScpy5Xs20n8e1qnNbdkLn0";
    /**
     * 阿里云发送短信
     * @param TemplateCode 代码模块代号
     * @param mobile 手机号码
     * @param code 验证码
     * @return 0 发送成功  -1发送失败
     *
     */
    public static int aliSendCode(String TemplateCode,String SignName, String mobile, String code) {
        int i = 0;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //区域代码---"cn-hangzhou"
        request.putQueryParameter("RegionId", "cn-hangzhou");
        //手机号码
        request.putQueryParameter("PhoneNumbers", mobile);
        //签名名称 五分钟洗车
        request.putQueryParameter("SignName", SignName);
        //代码模块代号  SMS_133215065
        request.putQueryParameter("TemplateCode", TemplateCode);
        //验证码
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            i = -1;
            e.printStackTrace();
            log.error("SMSUtils.ServerException阿里短信通道错误");
        } catch (ClientException e) {
            i = -1;
            e.printStackTrace();
            log.error("SMSUtils.ClientException阿里短信通道错误");
        }

        return i;
    }

    public static void main(String[] args) {
        aliSendCode("SMS_133215065","五分钟洗车","17512570148","13824");
    }

}


